#include "pch.h"
#include "Graphics.h"
#include "RTFHelper.h"
#include "BitmapHelper.h"

using namespace Windows::UI::Xaml::Controls;

Graphics::Graphics(void)
{
}


Graphics::~Graphics(void)
{
}

void Graphics::Initialize()
{
	//DirectXBase::Initialize(nullptr,nullptr,96);
}

void Graphics::Clear(UINT32 color)
{
	m_d2dContext->Clear(D2D1::ColorF(color));
}

void Graphics::StartDraw()
{
	m_d2dContext->BeginDraw();
}

void Graphics::EndDraw()
{
	m_d2dContext->EndDraw();
	DirectXBase::Present();
}

Windows::Storage::Streams::IRandomAccessStream^ Graphics::GetBitmapImage()
{
	/*ID2D1Image *image;
	m_d2dContext->GetTarget(&image);
	image->*/
	BitmapHelper::SaveD2DContextToFile(m_wicFactory,m_d2dContext,m_d2dTargetBitmap,this);
	return nullptr;
}

void Graphics::Render()
{
	RenderTarget();
}


//Draw Text
void Graphics::DrawText(Platform::String^ text,double x,double y,double width,double height)
{
	Platform::String^ rtf="{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}{\colortbl ;\red75\green172\blue198;\red247\green150\blue70;}{\*\generator Riched20 6.2.9200}\viewkind4\uc1 \pard\sa200\sl276\slmult1\cf1\f0\fs96\lang9 nc\cf0 vn\cf2 cv\cf0 n\fs22\par}";
	RTFHelper::ConvertRTF(rtf);
	HRESULT hr;
	IDWriteTextFormat *pTextFormat;
	DX::ThrowIfFailed(
	hr = m_dwriteFactory->CreateTextFormat(
        L"Gabriola",
        NULL,
        DWRITE_FONT_WEIGHT_REGULAR,
        DWRITE_FONT_STYLE_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL,
        72.0f,
        L"en-us",
        &pTextFormat
        )
	);
	D2D1_RECT_F drawRect={x,y,x+width,y+height};
	IDWriteTextLayout *textlayout;
	DX::ThrowIfFailed(
	m_dwriteFactory->CreateTextLayout(
		text->Data(),
		text->Length(),
		pTextFormat,
		40, // maxWidth.
		100, // maxHeight.
		&textlayout
		)
	);

	ID2D1SolidColorBrush *fillBrush;
	DX::ThrowIfFailed(
		m_d2dContext->CreateSolidColorBrush(D2D1::ColorF(0x00000000),&fillBrush)
	);

	m_d2dContext->DrawTextLayout(D2D1::Point2F(x,y),textlayout,fillBrush);
	pTextFormat->Release();
	textlayout->Release();
	fillBrush->Release();
}

//Draw Rectangle
void Graphics::DrawRectangle(double x,double y,double width,double height,ULONG32 fillColor,ULONG32 borderColor,double thickness)
{
	HRESULT hr;
	D2D1_RECT_F rect={x,y,x+width,y+height};
	ID2D1SolidColorBrush *fillBrush,*strokeBrush;
	hr=m_d2dContext->CreateSolidColorBrush( D2D1::ColorF(fillColor),&fillBrush);
	hr=m_d2dContext->CreateSolidColorBrush( D2D1::ColorF(borderColor),&strokeBrush);
	m_d2dContext->DrawRectangle(rect,strokeBrush,thickness);
	m_d2dContext->FillRectangle(rect,fillBrush);
}


